#!/bin/bash

set -e

# Script to generate e2e test child pipeline
# This is required because environment variables that are generated dynamically are not picked up by rules in child pipelines

source "$(dirname "$0")/utils.sh"
source $ENV_FILE

echoinfo "Generating child pipeline yml definitions for e2e test pipelines child pipelines"

declare -A qa_pipelines

# key/value pairs for qa pipeline yml definitions
qa_pipelines["package-and-test-pipeline.yml"]="package-and-test/main.gitlab-ci.yml"
qa_pipelines["package-and-test-nightly-pipeline.yml"]="package-and-test-nightly/main.gitlab-ci.yml"
qa_pipelines["review-app-pipeline.yml"]="review-apps/main.gitlab-ci.yml"
qa_pipelines["test-on-gdk-pipeline.yml"]="test-on-gdk/main.gitlab-ci.yml"

if [ "$QA_SKIP_ALL_TESTS" == "true" ]; then
  skip_pipeline=".gitlab/ci/_skip.yml"

  echoinfo "Using ${skip_pipeline} for all e2e test pipelines due to QA_SKIP_ALL_TESTS set to 'true'"
  for key in "${!qa_pipelines[@]}"; do
    cp $skip_pipeline "$key"
  done

  exit
fi

# set custom cache key to override default cache in pipeline-common because we use bundle to install gitlab-qa gem
qa_cache_key="qa-e2e-ruby-${RUBY_VERSION}-$(md5sum qa/Gemfile.lock | awk '{ print $1 }')"
# these variables are used across all qa child pipelines
# it allows to use all features across all child pipelines like skipping all tests, selective test execution etc
variables=$(cat <<YML
variables:
  GIT_DEPTH: "20"
  GIT_STRATEGY: "clone" # 'GIT_STRATEGY: clone' optimizes the pack-objects cache hit ratio
  GIT_SUBMODULE_STRATEGY: "none"
  GITLAB_QA_CACHE_KEY: "$qa_cache_key"
  GITLAB_SEMVER_VERSION: "$(cat VERSION)"
  QA_EXPORT_TEST_METRICS: "${QA_EXPORT_TEST_METRICS:-true}"
  QA_FEATURE_FLAGS: "${QA_FEATURE_FLAGS}"
  QA_FRAMEWORK_CHANGES: "${QA_FRAMEWORK_CHANGES:-false}"
  QA_RUN_ALL_TESTS: "${QA_RUN_ALL_TESTS:-false}"
  QA_RUN_ALL_E2E_LABEL: "${QA_RUN_ALL_E2E_LABEL:-false}"
  QA_SAVE_TEST_METRICS: "${QA_SAVE_TEST_METRICS:-false}"
  QA_SUITES: "$QA_SUITES"
  QA_TESTS: "$QA_TESTS"
  KNAPSACK_TEST_FILE_PATTERN: "$KNAPSACK_TEST_FILE_PATTERN"
YML
)

echo "***Saving generated qa pipeline files***"
for key in "${!qa_pipelines[@]}"; do
  echo "Generating $key"

  if [ "${CI_PROJECT_NAMESPACE}" = "gitlab-cn" ]; then
    cp "jh/.gitlab/ci/${qa_pipelines[$key]}" "$key"
  else
    cp ".gitlab/ci/${qa_pipelines[$key]}" "$key"
  fi

  echo >>"$key" # add empty line so it's easier to read if debugging
  echo "$variables" >>"$key"
done

echoinfo "Successfully generated qa pipeline files"
echo "$variables"
